package com.awardsystem.Controller;

import com.alibaba.fastjson.JSONObject;
import com.awardsystem.Service.ProjectService;
import com.awardsystem.Service.UserService;
import com.awardsystem.entity.Achievement;
import com.awardsystem.entity.Project;
import com.awardsystem.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class ProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    UserService userService;
    @PostMapping("/addProject")
    public JSONObject addAchievement(@RequestBody JSONObject params){
        JSONObject ret=new JSONObject();
        String name=params.getString("name");
        String projectNumber= params.getString("project_id");
        String level=params.getString("level");
        String category=params.getString("type");
        String leader=params.getString("manager");
        if(projectService.findProjectByNumber(projectNumber)!=null) {
            ret.put("success", false);
            ret.put("exc", "禁止重复添加");
        }else{
            Project project=new Project();
            project.setName(name);
            project.setNumber(projectNumber);
            project.setLeader(leader);
            project.setCategory(category);
            project.setLevel(level);
            projectService.save(project);
            ret.put("success",true);
            ret.put("exc","success");
        }
        return ret;
    }
    @GetMapping("/findProject")
    public JSONObject findProject(@RequestParam("name")String name){
        JSONObject ret=new JSONObject();
        List<Project> projectList1=new ArrayList<>();
        projectList1=projectService.findProjectsByName(name);
        Project projectList2=null;
        projectList2=projectService.findProjectByNumber(name);
        if(projectList2!=null)projectList1.add(projectList2);
        ret.put("success",true);
        ret.put("exc","success");
        ret.put("projects",projectList1);
        return ret;
    }
    @GetMapping("/getAllProject")
    public JSONObject getAllProject(){
        JSONObject ret=new JSONObject();
        List<Project> projectList=projectService.getAll();
        ret.put("success",true);
        ret.put("exc","success");
        ret.put("projects",projectList);
        return ret;
    }
    @PostMapping("/deleteProject")
    public JSONObject deleteProject(@RequestBody JSONObject params){
        JSONObject ret=new JSONObject();
        int id=Integer.parseInt(params.getString("id"));
        if(projectService.findById(id)==null) {
            ret.put("success", false);
            ret.put("exc", "未找到该项目");
            return ret;
        }else {
            Project project=projectService.findById(id);
            projectService.delete(project);
            ret.put("success",true);
            ret.put("exc","success");
        }
        return ret;
    }

    @PostMapping("/editProject")
    public JSONObject editProject(@RequestBody JSONObject params){
        JSONObject ret=new JSONObject();
        int id=Integer.parseInt(params.get("id").toString());
        Project project=projectService.findById(id);
        if(project==null){
            ret.put("success", false);
            ret.put("exc", "未找到该项目");
            return ret;
        }
        else{
            project.setNumber(params.getString("project_id"));
            project.setName(params.getString("name"));
            project.setLevel(params.getString("level"));
            project.setCategory(params.getString("type"));
            project.setLeader(params.getString("manager"));
            projectService.save(project);
            ret.put("success",true);
            ret.put("exc","成功");
        }
        return ret;
    }

    @GetMapping("/allUser")
    public JSONObject getAllUser(){
        JSONObject ret=new JSONObject();
        List<User> userList=userService.getAllUsers();
        ret.put("users",userList);
        ret.put("success",true);
        ret.put("exc","success");
        return ret;
    }


}
